package com.solarfridge.monitor.server;

import java.util.LinkedList;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.solarfridge.monitor.client.DataService;
import com.solarfridge.monitor.shared.data.Fridge;
import com.solarfridge.monitor.shared.data.FridgeData;

@SuppressWarnings("serial")
public class DataServiceImpl extends RemoteServiceServlet 
	implements DataService {
	
	@Override
	public int getNumFridges() {
		return 0;
	}

	@Override
	public Fridge[] getFridgeList() {	
		
		PersistenceManager pm = PMFactory.get().getPersistenceManager();		
		
		Query fridgeQuery = pm.newQuery(Fridge.class);
		@SuppressWarnings("unchecked")
		List<Fridge> fridgeList = (List<Fridge>) fridgeQuery.execute();
		
		Fridge[] fridgeArray = new Fridge[fridgeList.size()];
		
		for(int i = 0; i < fridgeList.size(); i++) {
			fridgeArray[i] = fridgeList.get(i);
		}
		
		return fridgeArray;
	}
	
	/**
	 * When used in the DataServiceImpl class, does not close the persistenceManager.
	 * This allows for the object to be modified.
	 */
	@Override
	public Fridge getFridgeById(String id) {
		PersistenceManager pm = PMFactory.get().getPersistenceManager();	

		Query query = pm.newQuery(Fridge.class);
		query.setFilter("mFridgeId == targetId");
		query.declareParameters("String targetId");		
		@SuppressWarnings("unchecked")
		List<Fridge> results = (List<Fridge>) query.execute(id);
		
		Fridge targetFridge;
		
		if(results.isEmpty()) {
			targetFridge = null;
		} else {
			targetFridge = results.get(0);
		}		
		
		return targetFridge;
	}

	@Override
	public int saveFridge(Fridge fridgeToSave) {
		PersistenceManager pm = PMFactory.get().getPersistenceManager();	
		int status;
		
		Fridge oldFridge = getFridgeById(fridgeToSave.getFridgeId());
		
		if(oldFridge == null){
			//means the id isn't already in use
			pm.makePersistent(fridgeToSave);
			status = DataService.SUCCESS;
		} else {
			status = DataService.NAME_TAKEN;
		}
		
		pm.close();			
		
		return status;
	}

	@Override
	public int recordData(FridgeData dataToRecord) {		
		PersistenceManager pm = PMFactory.get().getPersistenceManager();
		int status;
		
		Fridge fridgeDescriptor = getFridgeById(dataToRecord.getFridgeId());
		
		if(fridgeDescriptor != null) {
			pm.makePersistent(dataToRecord);			
			fridgeDescriptor.updateData(dataToRecord);
			status = DataService.SUCCESS;
		} else {
			status = DataService.NO_FRIDGE_FOUND;
		}		
		
		return status;
	}

	@Override
	public List<FridgeData> getRecentData(String fridgeId, int numRecords) {
		PersistenceManager pm = PMFactory.get().getPersistenceManager();
		
		Query query = pm.newQuery(FridgeData.class);
		query.setFilter("mFridgeId == targetId");
		query.declareParameters("String targetId");	
		query.setOrdering("mTimeStamp descending");
		query.setRange(0, numRecords);
		@SuppressWarnings("unchecked")
		List<FridgeData> results = (List<FridgeData>) query.execute(fridgeId);
		
		//Attempt to stop a serialization error
		List<FridgeData> transResult = new LinkedList<FridgeData>();
		transResult.addAll(results);
		
		return transResult;
	}

	@Override
	public int updateFridge(Fridge updateFridge) {
		
		if(getFridgeById(updateFridge.getFridgeId()) == null) {
			return DataServiceImpl.NO_FRIDGE_FOUND;
		} else {
			PersistenceManager pm = PMFactory.get().getPersistenceManager();
			pm.makePersistent(updateFridge);
			return DataServiceImpl.SUCCESS;
		}
	}
	
	
}
